/* eslint-disable */

const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const $ = require('gulp-load-plugins')();
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const merge = require('merge-stream');

const appDomain = require('./src/config').appDomain;
// set variable via $ gulp --type production
const environment = $.util.env.type || 'production';

const date = new Date();
const timestamp = ''
    + date.getFullYear()
    + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1))
    + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
    + (date.getHours() < 10 ? '0' + date.getHours() : date.getHours())
    + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
    + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());

const dist = 'dist/';

gulp.task('copyfile', () => {
    return gulp.src('client/static/**/*')
    .pipe(gulp.dest(dist));
});

gulp.task('webpackDllConfig', cb => {
    const webpackDllConfig = require('./webpack.config.dll.js').getConfig(environment, timestamp);
    webpack(webpackDllConfig, err => {
        if (err) {
            console.log('ERROR - ', err);
        }
        else {
            return cb();
        }
    });
});

gulp.task('webpackProdConfig', ['webpackDllConfig'], cb => {
    const webpackProdConfig = require('./webpack.config.prod.js').getConfig(environment, timestamp);
    webpack(webpackProdConfig, err => {
        if (err) {
            console.log('ERROR - ', err);
        }
        else {
            return cb();
        }
    });
});


gulp.task('copyForDevelop', () => {
    const staticFile = gulp.src('client/static/**/*')
        .pipe(gulp.dest(dist));

    const debugHTML = gulp.src('client/views/debug.html')
        .pipe($.rename('index.html'))
        .pipe(gulp.dest(dist));

    const indexHTML = gulp.src('client/views/debug.html')
        .pipe(gulp.dest(dist));

    return merge(staticFile, debugHTML, indexHTML);
});


// mkdir dist; cp client/static/fastclick.js dist; cp client/views/debug.html dist/;
// NODE_ENV=development webpack-dev-server --content-base ./dist --inline --hot --config webpack.config.js --host 0.0.0.0
gulp.task('webpackDevServer', () => {
    process.env.NODE_ENV = 'development';

    const devPort = '8080';
    const serverPort = '8103';
    const proxy = $.util.env.proxy === true
        ? 'https://qasandbox.bcetest.baidu.com'
        : ($.util.env.proxy || '');

    const webpackDevConfig = require('./webpack.config');
    webpackDevConfig.entry.index.unshift('webpack/hot/dev-server', 'webpack-dev-server/client?http://localhost:8080');
    webpackDevConfig.plugins.unshift(new webpack.HotModuleReplacementPlugin());
    const compiler = webpack(webpackDevConfig);
    new WebpackDevServer(compiler, {
        disableHostCheck: true,
        historyApiFallback: true,
        hot: true,
        contentBase: './dist',
        stats: {colors: true},
        disableHostCheck: true,
        proxy: {
            '/api/*': {
                debug: true,
                target: proxy || `http://127.0.0.1:${serverPort}/`,
                pathRewrite: proxy && proxy.indexOf('baidu.com') < 0 ? {'^/api/iot2/panda': ''} : {},
                secure: true,
                changeOrigin: true
            },
            '**/*.js': {
                debug: true,
                target: `http://127.0.0.1:${devPort}/`,
                pathRewrite(originPath, req) {
                    let newPath = originPath;

                    if (new RegExp(`^${appDomain}\/.*\\.js$`).test(originPath)) {
                        newPath = `/${path.basename(originPath)}`;
                    }

                    return newPath;
                },
                secure: true,
                changeOrigin: true
            }
        }
    }).listen(devPort, '0.0.0.0');
});

gulp.task('develop', ['copyForDevelop', 'webpackDevServer']);

gulp.task('compiletask', ['webpackProdConfig', 'copyfile']);

gulp.task('default', ['compiletask']);
